//
//  ShareViewController.m
//  TenMinDemo
//
//  Created by Shawn Li on 2021/3/10.
//  Copyright © 2021 CYXiang. All rights reserved.
//

#import "ShareViewController.h"
#import "ShareTabHeader.h"
#import "InviteUserTabCell.h"
#import "SimpleTabHeader.h"
#import "SharePopView.h"
#import "CYXBaseRequest.h"
#import "InviteDetailModel.h"

@interface ShareViewController ()
/// <#注释#>
@property (nonatomic, strong) InviteDetailModel *ivtModel;

/// <#注释#>
@property (nonatomic, strong) ShareTabHeader *header;
@end

@implementation ShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getIvtDetail];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)setupUI{
    [super setupUI];
    self.header = [[ShareTabHeader alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 520)];
    LRWeakSelf(self);
    self.header.actionBlock = ^(NSInteger index) {
        CYXLog(@"%zd",index);
        if(index == 0 ){
            LCCopyBoard(weakself.ivtModel.invitationCode);
            LCInfoHUD(@"邀请码已经复制", 1);
        }else if (index == 1){
            //            LCCopyBoard(weakself.ivtModel.invitationCode);
            LCInfoHUD(@"邀请链接已经复制", 1);
        }else if (index == 2){
            [weakself popShareView];
        }
    };
    self.tableView.tableHeaderView = self.header;
    [self.tableView registerNib:[UINib nibWithNibName:@"InviteUserTabCell" bundle:nil] forCellReuseIdentifier:@"InviteUserTabCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"SimpleTabHeader" bundle:nil] forHeaderFooterViewReuseIdentifier:@"SimpleTabHeader"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger s = 0;
    if (self.ivtModel.isHasSuper) {
        s += 1;
    }
    if (self.ivtModel.isHasSub) {
        s += 1;
    }
    return s;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (!section &&self.ivtModel.isHasSuper ) {
        return 1;
    }
    return self.ivtModel.subordinateAccounts.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    InviteAccountItem *accM;
    if (indexPath.section == 0 && self.ivtModel.isHasSuper) {
        accM = self.ivtModel.superiorAccount;
        accM.isSuper = YES;
    }
    if (indexPath.section == 1) {
        accM = self.ivtModel.subordinateAccounts[indexPath.row];
    }
    InviteUserTabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InviteUserTabCell"];
    if (!accM.icon) {
        [self getAccIcon:accM];
    }
    [cell refreshWith:accM];
    return cell;
    
}

- (void)getAccIcon:(InviteAccountItem *)accM{
    if (!accM.portraitKey || !accM.portraitKey.length) {
        return;
    }
    
    [CYXBaseRequest postWithUrl:API_getImageWithKey param:@{@"key":accM.portraitKey} resultClass:[UIImage class] success:^(id res) {
        if ([res isEqual: RequestErrorCode]) {
            
        }else{
            accM.icon = res;
            [self.tableView reloadData];
        }
        
        CYXLog(@"%@",res)
    } failure:^(NSError *error) {
        CYXLog(@"%@",error)
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    SimpleTabHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"SimpleTabHeader"];
    
    return header;
}

- (void)popShareView{
    
    SharePopView *view = [[SharePopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT)];
    LRWeakSelf(self);
    view.actionBlock = ^(NSInteger index) {
        CYXLog(@"%zd",index);
        [weakself showSysShare];
        //        if(index == 0 ){
        //
        //        }else if (index == 1){
        //
        //        }
    };
    
    [view showInView:self.navigationController.view];
    
}

- (void)showSysShare{
    
    UIImage *image = [UIImage imageNamed:[LanguageManager isEnglish] ? @"share_hb_en" :@"share_hb_zh"];
    //    NSURL *urlToShare = [NSURL URLWithString:@"http://blog.csdn.net/hitwhylz"];
    //    NSString *textToShare = @"要分享的文本内容";
    //    UIImage *imageToShare = [UIImage imageNamed:@"iosshare.jpg"];
    //    NSURL *urlToShare = [NSURL URLWithString:@"http://blog.csdn.net/hitwhylz"];
    NSArray *activityItems = @[image];
    
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    
    
    activityVC.completionWithItemsHandler = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
        NSLog(@"调用分享的应用id :%@", activityType);
        if (completed)
        {
            LCSuccessHUD(@"分享成功", 1);
            
        }
        else
        {
            LCInfoHUD(@"分享失败", 1);
        }
        
    };
    
    [self presentViewController:activityVC animated:YES completion:nil];
    
}

- (void)getIvtDetail{
    
    [CYXBaseRequest postWithUrl:API_invitationDetail param:nil resultClass:[InviteDetailModel class] success:^(id res) {
        if ([res isEqual: RequestErrorCode]) {
            
        }else{
            self.ivtModel = res;
            [self refreshView];
        }
        
        CYXLog(@"%@",res)
    } failure:^(NSError *error) {
        CYXLog(@"%@",error)
    }];
}

- (void)refreshView{
    [self.header refreshWith:self.ivtModel];
    [self.tableView reloadData];
}

@end
